package com.hndct.util;

import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class QiniuUtil {
	private static final Logger logger = LoggerFactory.getLogger(QiniuUtil.class);
	
	private static Configuration cfg = new Configuration(Zone.zoneAs0());
	private static UploadManager uploadManager = new UploadManager(cfg);
	
	//rabbit
	private static String accessKey = "oDzW84Kt8TLwdPv7VIbbMEsidjGOQ4B7i2o5NcLx";
	private static String secretKey = "UsoLlXh5DrWJxRzBqaJ5e95xJnPib4MO-Yhxr1D-";
	private static String bucket = "umpfile";
	
	private static String domain = "http://qiniu.ump.urbass.com/";
	private static String prefix = "img/ump/";
	
	/**
	 * @param filePath 本地文件的路径
	 * @param fileName 文件存到七牛后的名字
	 * @return 返回文件在七牛的访问路径
	 */
	public static String upload(String filePath,String fileName){
		try {
			Auth auth = Auth.create(accessKey, secretKey);
			String upToken = auth.uploadToken(bucket);
			
		    Response response = uploadManager.put(filePath, prefix+fileName, upToken);
		    logger.info("【七牛】上传返回：{}",JSONObject.fromObject(response).toString());
		    return domain+prefix+fileName;
		} catch (QiniuException ex) {
		    Response r = ex.response;
		    logger.info("【七牛】上传文件报错{}",r.toString());
		} catch (Exception e) {
		    logger.error("【七牛】上传文件报错{}",e);
		}
		
		return null;
	}
	
//	public static void main(String[] args) {
//		System.out.println(upload("c:/data/file/img/1545724517004_406245.png","img/ump/ump_test1.png"));
//	}

}
